/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package MVC;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

/**
 * TitleOnglet
 * Class specific for the setting of the title of a tab of conversation
 * @author Duy Duc, Metangmo
 */
public class TitleOnglet extends JPanel implements ActionListener  {
    
    private JLabel titre ;
    private JButton close;
    private JTabbedPane panelonglet;
    private ArrayList<String> selectedUsers;
    
    /**
     * Constructor
     * @param name
     *        Nickname corresponding with the chat tab
     * @param panelOnglet
     *        Associated chat tab
     * @param selectedUsers
     *        Chosen users list
     */
    public TitleOnglet( String name , JTabbedPane panelOnglet , ArrayList<String> selectedUsers) {
        
        this.setOpaque(false);
        titre =new JLabel(name);
        close =new JButton("x");
        panelonglet =  panelOnglet;
        close.setPreferredSize(new Dimension(10,10));
        close.addActionListener(this);
        this.add(titre);
        this.add(close);
        this.selectedUsers = selectedUsers;
    }
    
    
    /**
     * actionPerformed:
     * function performs the action of closing the chat tab
     * when the LocalUser click on the icon <<Close>> of the chat tab
     * @param ae
     *        ActionEvent
     */
    @Override
    public void actionPerformed(ActionEvent ae) {
       
        if (ae.getSource() == close){
            
            System.out.println(" I clicked on the icon <<Close>> ");
            panelonglet.remove(panelonglet.getSelectedIndex());
        }

            Iterator<String> itr = selectedUsers.iterator();
            while(itr.hasNext()){
              String mess = itr.next();
              if ( mess == titre.getText())
              itr.remove();
            } 
     }
           
}

		     
    
